package com.toune.movie.base.myUtil;

import android.text.TextUtils;

import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Response;

import java.util.HashMap;
import java.util.Map;

public abstract class NoHttpCallBack<T> implements OnResponseListener<String> {

    public static Map<String, Object> params = new HashMap<>();

    private Class<T> tClass;

    public NoHttpCallBack(Class<T> tClass) {
        this.tClass = tClass;
    }

    @Override
    public void onStart(int what) {

    }

    @Override
    public void onFailed(int what, Response<String> response) {
        getFailed(response.get());
    }

    @Override
    public void onSucceed(int what, Response<String> response) {
        String json = response.get();
        //如果是字符串类型，则直接把json内容返回
        if (tClass==String.class){
            getSucceed((T)json);
            return;
        }
        if (!TextUtils.isEmpty(json)) {
            T t = GsonBinder.toObj(json, tClass);
            getSucceed(t);
        }
    }

    @Override
    public void onFinish(int what) {
        params.clear();
    }

    //服务器返回的json串转换成View的方法，可以在使用回调对象时自己实现这个方法
    protected abstract void getSucceed(T t);

    protected abstract void getFailed(String json);

}
